<?php

namespace Response;

use Workerman\Connection\TcpConnection;

abstract class Response
{
    /**
     *
     * @var int 业务状态码
     */
    protected $subCode;

    /**
     * @var string 响应说明
     */
    protected $statusMessage;

    /**
     * @var mixed 相关数据
     */
    protected $data;

    public function __construct($code, $subCode, $statusMessage, $data = null)
    {
        $this->code = $code;
        $this->subCode = $subCode;
        $this->statusMessage = $statusMessage;
        $this->data = null;
    }

    /**
     * 准备数据
     *
     * @return mixed
     */
    public abstract function prepareData($data);

    public function send(TcpConnection $connection)
    {
        $data = [
            'code' => $this->code,
            'subCode' => $this->subCode,
            'message' => $this->statusMessage,
            'payload' => $this->data
        ];

        return $connection->send($this->prepareData($data));
    }
}
